<template>
  <div class="login">
    <!-- 头部 -->
    <div class="login_bg">
      <!-- 头部背景图 -->
      <img
        src="@/assets/bgImage.jpg"
        alt=""
        class="login_img"
      />
      <!-- logo -->
      <img
        src="@/assets/photo.jpg"
        alt=""
        class="logo_img"
      />
    </div>
    <!-- 输入框 -->
    <div class="content">
      <van-form ref="form">
        <!-- 手机号 -->
        <van-field
          v-model="form.mobile"
          :rules="rules.mobile"
          name="mobile"
          placeholder="请输入手机号"
          class="inputCss1"
        >
          <template #left-icon>
            <i class="iconfont icon-mobile-phone"
              ><span class="zone">+86</span></i
            >
          </template>
        </van-field>
        <!-- 验证码 -->
        <van-field
          v-model="isCode"
          :rules="rules.code"
          round
          block
          placeholder="请输入验证码"
          class="inputCss2"
        >
          <template #left-icon>
            <i class="iconfont icon-lock"></i>
          </template>
          <template #button>
            <span class="code" @click="btnCode" v-if="codeTime === 10"
              >获取验证码</span
            >
            <span class="code" v-else>{{ codeTime }}s重新获取</span>
          </template>
        </van-field>
      </van-form>
    </div>
    <!-- 登录按钮 -->
    <div style="margin: 16px;" class="submit">
      <van-button
        round
        block
        type="info"
        native-type="submit"
        @click="btnLogin"
        class="submitBtn"
        >登陆</van-button
      >
      <div class="btnbottom">
        <div class="bot1">忘记密码？</div>
        <div class="bot2">立即注册</div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="footer">
      <div class="rest">
        <span class="hengxian"></span><small>其他登录方式</small
        ><span class="hengxian"></span>
      </div>
      <div class="footer_img">
        <img
          src="@/assets/01-登录注册移动端app界面/01-登录注册移动端app界面_21.png"
          alt=""
          class="img1"
        />
        <img
          src="@/assets/01-登录注册移动端app界面/01-登录注册移动端app界面_23.png"
          alt=""
          class="img2"
        />
        <img
          src="@/assets/01-登录注册移动端app界面/01-登录注册移动端app界面_25.png"
          alt=""
          class="img3"
        />
      </div>
    </div>
  </div>
</template>

<script>
// 导入登陆接口
import { authorizations } from '@/api/login.js'
import { setLocal } from '@/utils/Local.js'
import axios from 'axios'
export default {
  data () {
    return {
      // 获取验证码的倒计时
      codeTime: 10,
      isCode: '',
      mockCode: null,
      form: {
        mobile: '13999999999', // 手机号
        code: '' // 验证码
      },
      rules: {
        // 手机校验
        mobile: [
          { required: true, message: '请填写手机号', tirgger: 'onchang' },
          {
            validator: val => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                val
              )
            },
            message: '请正确输入手机号',
            tirgger: 'onchang'
          }
        ],
        // 验证码校验
        code: [
          {
            required: true,
            message: '请填写验证码',
            tirgger: 'onSubmit'
          }
        ]
      }
    }
  },
  methods: {
    // 获取验证码
    btnCode () {
      // console.log(123)
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // console.log(123)
          this.$toast.loading({
            duration: 0
          })
          // 倒计时处理
          this.codeTime--
          const tocalTiemId = setInterval(() => {
            this.codeTime--
            if (this.codeTime <= 0) {
              // 还原时间
              this.codeTime = 10
              // 清除计时器
              clearInterval(tocalTiemId)
            }
          }, 1000)
          axios({
            url: '/mockCode'
          }).then(res => {
            this.$toast.success(res.data.data.code)
            this.isCode = res.data.data.code
            console.log(res)
            this.mockCode = res.data.data.code
            this.isCode = res.data.data.code
          })
          // console.log('校验成功')
        })
        .catch(() => {})
    },
    // 登陆
    btnLogin () {
      // console.log(this.isCode, this.mockCOde)
      console.log(Number(this.isCode) === Number(this.mockCode))
      if (Number(this.isCode) === Number(this.mockCode)) {
        //  赋值给code
        this.form.code = '246810'
        // 发送axios请求
        this.$refs.form.validate().then(() => {
          authorizations(this.form).then(res => {
            this.$toast.success('登陆成功')
            setLocal('token', res.data.token)
            // this.$store.dispatch('refreshUserInfo')
            this.$store.commit('setIsLogin', true)
            const redirectPath = this.$route.query.redirect || '/home/my'
            this.$router.push(redirectPath)
          })
        })
      } else {
        this.$toast.fail('验证码失败')
      }
    }
  }
}
</script>

<style lang="less">
.login {
  // 顶部背景徒
  .login_bg {
    position: relative;
    .login_img {
      width: 100%;
    }
    .logo_img {
      width: 75px;
      height: 75px;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      bottom: -33px;
      transform: translateX(-50%);
    }
  }
  // 输入框
  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px white inset; /**通过边框阴影遮挡住背景*/
    -webkit-text-fill-color: #333;
  }
  .content {
    margin-top: 50px;
    padding: 0 @p15;
    .inputCss1 {
      margin-bottom: 20px;
      outline: none;
      .iconfont {
        font-size: 25px;
        display: flex;
        align-items: center;
        justify-content: center;
        .zone {
          display: inline-block;
          font-size: 15px;
          margin-left: 5px;
          margin-right: 5px;
          padding-right: 8px;
          height: 20px;
          line-height: 20px;
          border-right: 2px solid #ccc;
        }
      }
    }
    .inputCss2 {
      align-items: center;
      .iconfont {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 25px;
        margin-right: 5px;
      }
      .code {
        display: block;
        text-align: center;
        color: white;
        width: 100px;
        height: 30px;
        line-height: 30px;
        // background: linear-gradient(to right, #ff6251 0%, #ff9d55 100%);
        background: linear-gradient(to right, #f774bc 0%, #f3bedf 100%);
        border: none;
        border-radius: 30px;
      }
    }
  }
  // 登陆按钮
  .submit {
    .submitBtn {
      border: none;
      // background: linear-gradient(to right, #ff6251 0%, #ff9d55 100%);
      background: linear-gradient(to right, #f774bc 0%, #f3bedf 100%);
      font-family: NotoSansHans-Medium;
      font-size: 18px;
      font-weight: normal;
      font-stretch: normal;
      line-height: 21px;
      letter-spacing: 0px;
      color: #ffffff;
      margin-bottom: 20px;
    }
    .btnbottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 30px;
      .bot1 {
        width: 100px;
        height: 12px;
        font-family: NotoSansHans-Medium;
        font-size: 12px;
        font-weight: bold;
        font-stretch: normal;
        line-height: 12px;
        letter-spacing: 0px;
        color: #b3b3b3;
      }
      .bot2 {
        width: 60px;
        height: 12px;
        font-family: NotoSansHans-Medium;
        font-size: 12px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 12px;
        letter-spacing: 0px;
        color: #ff6f52;
      }
    }
  }
  // 底部
  .footer {
    margin-top: 50px;
    text-align: center;
    .rest {
      display: flex;
      align-items: center;
      padding: 0 @p15;
      small {
        width: 100px;
        height: 12px;
        font-family: NotoSansHans-Medium;
        font-size: 12px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 12px;
        letter-spacing: 0px;
        color: #969696;
        margin-left: 5px;
        margin-right: 5px;
      }
      .hengxian {
        display: block;
        width: 135px;
        height: 1px;
        background-color: #969696;
      }
    }
    .footer_img {
      margin-top: 30px;
      img {
        width: 47px;
        height: 47px;
        border-right: 50%;
      }
      .img2 {
        margin-left: 30px;
        margin-right: 30px;
      }
    }
  }
}
</style>
